package cn.cdeden.aiqa.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cdeden.common.idempotent.annotation.RepeatSubmit;
import cn.cdeden.common.log.annotation.Log;
import cn.cdeden.common.web.core.BaseController;
import cn.cdeden.common.mybatis.core.page.PageQuery;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.validate.AddGroup;
import cn.cdeden.common.core.validate.EditGroup;
import cn.cdeden.common.log.enums.BusinessType;
import cn.cdeden.common.excel.utils.ExcelUtil;
import cn.cdeden.aiqa.domain.vo.WikiModelVo;
import cn.cdeden.aiqa.domain.bo.WikiModelBo;
import cn.cdeden.aiqa.service.IWikiModelService;
import cn.cdeden.common.core.utils.page.TableDataInfo;

/**
 * 模型管理
 * 前端访问路由地址为:/aiqa/model
 *
 * @author Nick
 * @date 2025-02-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/model")
public class WikiModelController extends BaseController {

    private final IWikiModelService wikiModelService;

    /**
     * 查询模型管理列表
     */
    @SaCheckPermission("aiqa:model:list")
    @GetMapping("/list")
    public TableDataInfo<WikiModelVo> list(WikiModelBo bo, PageQuery pageQuery) {
        return wikiModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出模型管理列表
     */
    @SaCheckPermission("aiqa:model:export")
    @Log(title = "模型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WikiModelBo bo, HttpServletResponse response) {
        List<WikiModelVo> list = wikiModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "模型管理", WikiModelVo.class, response);
    }

    /**
     * 获取模型管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("aiqa:model:query")
    @GetMapping("/{id}")
    public R<WikiModelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wikiModelService.queryById(id));
    }

    /**
     * 新增模型管理
     */
    @SaCheckPermission("aiqa:model:add")
    @Log(title = "模型管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WikiModelBo bo) {
        return toAjax(wikiModelService.insertByBo(bo));
    }

    /**
     * 修改模型管理
     */
    @SaCheckPermission("aiqa:model:edit")
    @Log(title = "模型管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WikiModelBo bo) {
        return toAjax(wikiModelService.updateByBo(bo));
    }

    /**
     * 删除模型管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("aiqa:model:remove")
    @Log(title = "模型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wikiModelService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据当前用户角色获取模型
     *
     * @param id 用户id
     */
    @GetMapping("/get")
    @Log(title = "模型管理:根据当前用户角色", businessType = BusinessType.OTHER)
    public R<List<WikiModelVo>> get(@RequestParam Long id) {
        return R.ok(wikiModelService.getAll(id));
    }
}
